package com.bipa.controller;

import com.bipa.bean.Article;
import com.bipa.service.ArticleService;
import com.bipa.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @Autowired
    ArticleTypeService articleTypeService;

    @RequestMapping("removeOrPutArticle")
    public ModelAndView removeOrPut(@RequestParam("id")Integer id,@RequestParam("disabled")Integer disabled){
        ModelAndView mv = new ModelAndView();
        articleService.removeOrPutArticle(id,disabled);
        mv.setViewName("redirect:./mindex");
        return mv;
    }

    @RequestMapping("preArticle")
    public ModelAndView preArticle(@RequestParam("id")Integer id){
        ModelAndView mv = new ModelAndView();
        mv.addObject("article",articleService.getArticleById(id));
        mv.setViewName("back/preArticle");
        return mv;
    }

    @RequestMapping(value = "articleUpdate",method = RequestMethod.GET)
    public ModelAndView showArticle(@RequestParam("id")Integer id){
        ModelAndView mv = new ModelAndView();
        mv.addObject("article",articleService.getArticleById(id));
        mv.addObject("types",articleTypeService.findFirstTypes());
        mv.setViewName("back/updateArticle");
        return mv;
    }

    @RequestMapping(value = "articleUpdate",method = RequestMethod.POST)
    public ModelAndView showArticle(@ModelAttribute Article article, @RequestParam("pic") MultipartFile file ,HttpServletRequest request) throws IOException, ServletException {
        ModelAndView mv = new ModelAndView();
        /*
        //获取Part对象   文件的信息
        Part part = request.getPart("pic");
        //获取文件的原始名称    1.jpg
        System.out.println(part);
        String fileName = part.getSubmittedFileName();
        */
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        String image = article.getImage();
        System.out.println(image);
        if(fileName != null && !fileName.equals("")) {
            //通过UUID生成新的文件名
            image = UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
            System.out.println("新的文件名:"+image);

            //指定要上传的路径
            String path = request.getServletContext().getRealPath("/static/image/article");
            System.out.println("path:"+path);
            file.transferTo(new File(path,image));
            //part.write(path+ File.separator+image);
        }
        article.setImage(image);
        articleService.update(article);
        mv.setViewName("redirect:./mindex");
        return mv;
    }

    @RequestMapping("addArticle")
    public ModelAndView save(@ModelAttribute Article article,@RequestParam("pic")MultipartFile file,HttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        String fileName = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
        String path = request.getServletContext().getRealPath("/static/image/article");
        file.transferTo(new File(path,newFileName));
        article.setImage(newFileName);
        articleService.insert(article);
        mv.setViewName("redirect:./mindex");
        return mv;
    }
}
